﻿#if No_No_no_Error
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
	public GameObject[] levelList;

	public GameObject[] levelNewModeList;

	public GameObject[] ballList;

	public static GameManager _instance;

	public int _star, _ball;

	public bool isGameOver, isClear;

	public Color[] _color;

	public int _mode;

	public GameObject wall1, wall2, wall3, wall4;

	private float originalX, originalY;

	void Awake ()
	{
		_instance = this;
		_mode = PlayerPrefs.GetInt ("Mode");
		Debug.Log (_mode);
		GetLevels ();
		Application.targetFrameRate = 60;
	}

	public void GetLevels ()
	{
		int currentLevel = PlayerPrefs.GetInt ("CurrentLevel");
		if (_mode == 0)
			Instantiate (levelList [currentLevel - 1], Vector3.zero, Quaternion.Euler (0, 0, 0));
		else
			Instantiate (levelNewModeList [currentLevel - 1], Vector3.zero, Quaternion.Euler (0, 0, 0));
	}

	void RandomColor ()
	{
		int _random = Random.Range (0, 5);
		Camera.main.backgroundColor = _color [_random];
	}

	// Use this for initialization
	void Start ()
	{
		isGameOver = false;
		_star = 0;
		_ball = 0;
		isClear = false;
		ballList = GameObject.FindGameObjectsWithTag ("Ball");
		if (_mode == 0)
			DeActiveBalls ();
		else {
			ActiveBalls ();
			SetWall ();
		}
		RandomColor ();
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (CheckBlackBallInTarget () && !isClear) {

			isClear = true;
			UIManager._uiManager.ShowGameOver ();
		}
		
		if (CheckBallsInTarget () && !isClear) {

			isClear = true;
			if (_mode == 0)
				CheckNoTimer ();
			else
				StartCoroutine (CheckInTimer ());

		}
	}

	IEnumerator CheckInTimer ()
	{
		yield return new WaitForSeconds (2.0f);
		if (!CheckBlackBallInTarget ())
			UIManager._uiManager.ShowClear ();
		else
			UIManager._uiManager.ShowGameOver ();
	}

	void CheckNoTimer ()
	{
		
		if (!CheckBlackBallInTarget ())
			UIManager._uiManager.ShowClear ();
		else
			UIManager._uiManager.ShowGameOver ();
	}

	public void DeActiveBalls ()
	{
		foreach (GameObject _ball in ballList)
			_ball.GetComponent<Rigidbody2D> ().gravityScale = 0.0f;
	}

	public void ActiveBalls ()
	{
		foreach (GameObject _ball in ballList)
			_ball.GetComponent<Rigidbody2D> ().gravityScale = 2.0f;
	}

	public void GetStar ()
	{
		_star++;
	}

	public bool CheckBallsInTarget ()
	{
		bool _check = true;

		foreach (GameObject _ball in ballList) {
			if (_mode == 1) {
				if (!_ball.GetComponent<Ball> ().hitTarget && _ball.name == "Ball2")
					_check = false;
			} else {
				if (!_ball.GetComponent<Ball> ().hitTarget)
					_check = false;
			}
		}

		return _check;
	}

	public bool CheckBlackBallInTarget ()
	{
		bool _check = false;

		foreach (GameObject _ball in ballList) {

			if (_ball.GetComponent<Ball> ().hitTarget && _ball.name == "Ball3")
				_check = true;
		}

		return _check;
	}

	void SetWall ()
	{
		originalX = -Camera.main.orthographicSize * Camera.main.aspect;
		originalY = 5.0f;
		wall1.SetActive (true);
		wall2.SetActive (true);
		wall3.SetActive (true);
		wall4.SetActive (true);
		wall1.transform.position = new Vector3 (originalX - 0.5f, 0.0f, 0.0f);
		wall2.transform.position = new Vector3 ((float)(Camera.main.orthographicSize * Camera.main.aspect) + 0.5f,
			0.0f, 0.0f);
	}
}
#endif